package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.comon.CustomException;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.mapper.SetmealMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.cookie.SM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 七安
 * @description 针对表【setmeal(套餐)】的数据库操作Service实现
 * @createDate 2022-08-12 16:37:53
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
        implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    //分页查询
    @Override
    public Page<Setmeal> findPage(Integer page, Integer pageSize, String name) {
        //条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //分页查询器
        Page<Setmeal> setmealPage = new Page<Setmeal>(page, pageSize);
        setmealPage = setmealMapper.selectPage(setmealPage, wrapper);
        //获取分页结果
        List<Setmeal> setmealList = setmealPage.getRecords();
        //获取对应的套餐名字
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //根据套餐id查询对应的名字
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
            }
        }
        return setmealPage;
    }

    //添加套餐
    @Override
    public void saveSetmeal(Setmeal setmeal) {
        setmealMapper.insert(setmeal);
        //获取菜品集合
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        //遍历集合
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                //获取套餐id并添加
                setmealDish.setSetmealId(setmeal.getId().toString());
                //添加到数据库中
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //根据ID查询
    @Override
    public Setmeal findById(Long id) {
        //根据id进行查询
        Setmeal setmeal = setmealMapper.selectById(id);
        //在查对应的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        //将查回来的集合封装到setmeal中
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;
    }

    //修改
    @Override
    public void updateSetmeal(Setmeal setmeal) {
        setmealMapper.updateById(setmeal);
        //获取菜品
        List<SetmealDish> newSetmealDish = setmeal.getSetmealDishes();
        //获取之前的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> oldSetmealDish = setmealDishMapper.selectList(wrapper);
        //创建临时集合,存储新老相同的菜品
        List<SetmealDish> tempList = new ArrayList<>();
        //循环遍历
        for (SetmealDish newDish : newSetmealDish) {
            for (SetmealDish oldDish : oldSetmealDish) {
                //如果新老名字相同
                if (StringUtils.equals(newDish.getName(), oldDish.getName())) {
                    //老id换新id
                    newDish.setId(oldDish.getId());
                    //新菜改旧菜
                    setmealDishMapper.updateById(newDish);
                    //相同的添加到临时集合中
                    tempList.add(oldDish);
                }
            }
            if (newDish.getId() == null) {
                newDish.setSetmealId(setmeal.getId().toString());
                //添加
                setmealDishMapper.insert(newDish);
            }
        }
        //删除调老菜品和临时集合中重复的
        oldSetmealDish.removeAll(tempList);
        //将老集合中多余的删除
        for (SetmealDish setmealDish : oldSetmealDish) {
            setmealDishMapper.deleteById(setmealDish.getId());
        }
    }

    //删除
    @Override
    public void deleteSetmeal(List<Long> ids) {
        //查询在售状态
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //1为启售状态
        wrapper.eq(Setmeal::getStatus, 1);
        wrapper.in(Setmeal::getIsDeleted, ids);
        Long count = setmealMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("当前还处于在售状态,请停售后重试");
        }

        //先删从表的菜品
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper1);
        //再删主表的套餐
        setmealMapper.deleteBatchIds(ids);
    }

    //app端查询套餐
    @Override
    public List<Setmeal> findSetmeal(Long categoryId, Integer status) {
        //条件封装
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status);
        /* //查询套餐里的菜品
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                LambdaQueryWrapper<SetmealDish> sdWrapper = new LambdaQueryWrapper<>();
                sdWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(sdWrapper);
                //将结果封装到setmeal对象
                setmeal.setSetmealDishes(setmealDishes);
            }
        }*/
        return setmealMapper.selectList(wrapper);
    }

    //点击套餐查看里面的菜品
    @Override
    public List<Dish> findDish(Long id) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        List<Dish> dishes = new ArrayList<>();
        //根据dish_id查询dish
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
                dishWrapper.eq(Dish::getId, setmealDish.getDishId());
                //查询
                Dish dish = dishMapper.selectOne(dishWrapper);
                //设置份数
                dish.setCopies(setmealDish.getCopies().toString());
                dishes.add(dish);
            }
        }
        return dishes;
    }
}




